package com.petShop.controllers;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.petShop.dao.UserDao;
import com.petShop.models.Userinfo;
import com.petShop.util.MD5Utils;

@Controller
@Transactional
@RequestMapping("/registe")
public class RegisteController {
	@Autowired
	UserDao userDao;
	@RequestMapping(method=RequestMethod.GET)
	public ModelAndView get(){
		ModelAndView mav = new ModelAndView("/registe/registe");
		return mav;
	}
	
	@RequestMapping(method=RequestMethod.POST)
	public ModelAndView post(@RequestParam String username,@RequestParam String password,
			HttpSession session){
		ModelAndView mav = new ModelAndView();
		Userinfo userinfo = userDao.selectUserByName(username);
		if(userinfo!=null){
			mav.addObject("errorMsg","用户名已被注册");
			mav.setViewName("/registe/registe");
		}else{
			Userinfo u = new Userinfo();
			u.setUsername(username);
			u.setPassword(MD5Utils.md5Password(password));
			userDao.insertUser(u);
			session.setAttribute("username", username);
			mav.setViewName("redirect:/registe/reSuccess");
		}
		return mav;
	}
	
	@RequestMapping(value="/reSuccess",method=RequestMethod.GET)
	public ModelAndView reSuccess(){
		ModelAndView mav = new ModelAndView("/registe/reSuccess");
		return mav;
	}
}
